package cn.test.servlet;

import cn.test.entity.User;
import cn.test.service.LoginService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by sam on 2017/11/23.
 */
@WebServlet(urlPatterns = "/usercheck.do")
public class UserCheckServlet extends HttpServlet {
    private static final long serialVersionUID =1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符编码集
        req.setCharacterEncoding("utf-8");
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mvc.xml");
        DataSource ds = ac.getBean("ds", DataSource.class);
        LoginService loginService = ac.getBean("loginService", LoginService.class);
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username+" "+password);
        User user = loginService.checkLogin(username, password);

        //响应前台
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        if (user!=null) {
            req.setAttribute("aaa","567");
            //req.getRequestDispatcher("success.jsp").forward(req,resp);
        }
        out.flush();
        out.close();
    }
}
